package com.wugui.datax.admin.controller;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.WorkbookUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.api.R;
import com.wugui.datatx.core.biz.model.ReturnT;
import com.wugui.datax.admin.entity.JobDatasource;
import com.wugui.datax.admin.entity.JobDatasourceFile;
import com.wugui.datax.admin.service.JobDatasourceFileService;
import com.wugui.datax.admin.util.FileConstants;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

/****************************************
 *
 *@Author: wangzp
 *@Date: 2023/7/28 周五 11:13
 *@Version: v1.0
 *@Desc: File数据源配置控制器层
 *
 *****************************************/
@RestController
@RequestMapping("/api/jobFileDatasource")
@Api(tags = "File数据源配置接口")
@Slf4j
public class JobDatasourceFileController extends BaseController{



    @Autowired
    JobDatasourceFileService datasourceFileService;

    /**
     * 上传文件
     * @param file
     * @return
     * @throws IOException
     */
    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public ReturnT<String> upload(MultipartFile file) throws Exception {


        return this.datasourceFileService.upload(file);

    }

    /**
     * 根据数据源ID查询文件列表
     * @param id
     * @return
     */
    @ApiOperation("根据数据源ID查询文件列表")
    @GetMapping("/getfiles/{id}")
    public ReturnT<List<JobDatasourceFile>> getFilesByDatasourceId(@PathVariable(value = "id") Long id){
        List<JobDatasourceFile> files = datasourceFileService.getFilesByDatasourceId(id);
        return new ReturnT<>(files);
    }

    /**
     * 根据文件ID获取文件表头
     * @param id
     * @return
     * @throws Exception
     */
    @ApiOperation("根据文件ID获取文件表头")
    @GetMapping("/getColumns/{id}")
    public ReturnT<List<Object>> getTitleById(@PathVariable(value = "id") Long id) throws Exception{
        JobDatasourceFile datasourceFile = datasourceFileService.getById(id);
        File xlsFile = new File(datasourceFile.getFilePath());

        InputStream inputStream = new FileInputStream(xlsFile);

        ExcelReader reader = ExcelUtil.getReader(inputStream);

        List<Object> header = reader.readRow(0);

        return new ReturnT<>(header);

    }

    /**
     * 根据文件ID获取文件内容
     * @return
     * @throws Exception
     */
    @ApiOperation("根据文件ID获取文件内容")
    @GetMapping("/getData/{id}")
    public ReturnT<List<Map<String,Object>>> getDataById(@PathVariable(value = "id") Long id) throws Exception{
        JobDatasourceFile datasourceFile = datasourceFileService.getById(id);
        File xlsFile = new File(datasourceFile.getFilePath());

        InputStream inputStream = new FileInputStream(xlsFile);

        ExcelReader reader = ExcelUtil.getReader(inputStream);
        return new ReturnT<>(reader.readAll());
    }

}
